using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.ObjectBuilder;
using GC2.ExceptionHandler;
using GC2.ExceptionHandler.Services;
using GC2.Entities.Logistica;
using GC2.DataAccess;

namespace GC2.DataAccess.DALogistica
{
    public class DACombustible
    {
        private Database _database;        // Objeto DataBase del Data Access Application Block

        #region Propiedades miembro
        /// <summary>
        /// Seteo la propiedad _database de la clase.
        /// </summary>
        private Database DataBase
        {
            get { return _database; }
            set { _database = value; }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Contructor de la clase sin parametro. Crea un objeto DataBase
        /// del Data AppBlock tomando el defaultDatabase del app.config.
        /// </summary>
        //public AplicacionesDataAccess()
        //{
        //    this.DataBase = DatabaseFactory.CreateDatabase();
        //}
        #endregion

        #region DACombustibleProximo
        /// <summary>
        /// Devuelve el proximo numero de Item de un combustible en base a una Patente
        /// </summary>
        /// <param name="Connection"></param>
        /// <param name="Combustible"></param>
        /// <returns name="Combustible"></returns>
        public Combustible DACombustibleProximo(string Connection, Combustible combustible)
        {
            this.DataBase = DatabaseFactory.CreateDatabase(Connection);
            DbCommand cmd = DataBase.GetStoredProcCommand("ProximoIDConClave");

            try
            {
                using (cmd)
                {
                    String sParam = "'" + combustible.Patente + "'";
                    DataBase.AddInParameter(cmd, "@tabla", DbType.String, "Combustible");
                    DataBase.AddInParameter(cmd, "@campo", DbType.String, "Item");
                    DataBase.AddInParameter(cmd, "@clave", DbType.String, "Patente");
                    DataBase.AddInParameter(cmd, "@param", DbType.String, sParam);
                    IDataReader Odbreader = DataBase.ExecuteReader(cmd);
                    while (Odbreader.Read())
                    {
                        combustible.Item = Convert.ToInt16(Odbreader["ID_Item"]);
                    }

                    return combustible;
                }
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
        }
        #endregion

        #region DACombustibleInsert
        /// <summary>
        /// Inserta un nuevo combustible
        /// </summary>
        /// <param name="Connection"></param>
        /// <param name="Combustible"></param>
        public void DACombustibleInsert(string Connection, Combustible combustible)
        {
            this.DataBase = DatabaseFactory.CreateDatabase(Connection);
            DbCommand cmd = DataBase.GetStoredProcCommand("Combustible_Insert");

            try
            {
                using (cmd)
                {
                    DataBase.AddInParameter(cmd, "@Patente", DbType.String, combustible.Patente);
                    DataBase.AddInParameter(cmd, "@Sucursal", DbType.Int16, combustible.Sucursal);
                    DataBase.AddInParameter(cmd, "@Item", DbType.Int16, combustible.Item);
                    DataBase.AddInParameter(cmd, "@FechaCarga", DbType.DateTime, combustible.FechaCarga);
                    DataBase.AddInParameter(cmd, "@Litros", DbType.Decimal, combustible.Litros);
                    DataBase.AddInParameter(cmd, "@Id_Usuario", DbType.Int16, combustible.Id_Usuario);

                    DataBase.ExecuteNonQuery(cmd);
                }
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
        }
        #endregion

        #region DACombustibleGetAll
        /// <summary>
        /// Devuelve Lista de Combustible
        /// </summary>
        /// <param name="Connection"></param>
        /// <param name="Combustible"></param>
        /// <returns>Combustible</returns>
        public List<Combustible> DACombustibleGetAll(string Connection, Combustible combustibleD, Combustible combustibleH)
        {
            this.DataBase = DatabaseFactory.CreateDatabase(Connection);
            DbCommand cmd = DataBase.GetStoredProcCommand("Combustible_GetAll");

            List<Combustible> listCombustible = new List<Combustible>();
            try
            {
                using (cmd)
                {
                    DataBase.AddInParameter(cmd, "@Sucursal", DbType.Int16, combustibleD.Sucursal);
                    DataBase.AddInParameter(cmd, "@FechaD", DbType.DateTime, combustibleD.FechaCarga);
                    DataBase.AddInParameter(cmd, "@FechaH", DbType.DateTime, combustibleH.FechaCarga);
                    IDataReader Odbreader = DataBase.ExecuteReader(cmd);
                    while (Odbreader.Read())
                    {
                        Combustible combustible = new Combustible();
                        combustible.Patente = (string)Odbreader["Patente"];
                        combustible.Sucursal = (int)Odbreader["Sucursal"];
                        combustible.Item = (int)Odbreader["Item"];
                        combustible.FechaCarga = (DateTime)Odbreader["FechaCarga"];
                        combustible.Litros = (Decimal)Odbreader["Litros"];
                        combustible.Id_Usuario = (int)Odbreader["Id_Usuario"];

                        listCombustible.Add(combustible);
                    }

                    return listCombustible;
                }
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
        }
        #endregion

        #region DACombustibleGetAllByPatente
        /// <summary>
        /// Devuelve Lista de Combustible de un Rodado
        /// </summary>
        /// <param name="Connection"></param>
        /// <param name="Combustible"></param>
        /// <returns>Combustible</returns>
        public List<Combustible> DACombustibleGetAllByPatente(string Connection, Combustible combustibleD, Combustible combustibleH)
        {
            this.DataBase = DatabaseFactory.CreateDatabase(Connection);
            DbCommand cmd = DataBase.GetStoredProcCommand("Combustible_GetAllByPatente");

            List<Combustible> listCombustible = new List<Combustible>();
            try
            {
                using (cmd)
                {
                    DataBase.AddInParameter(cmd, "@Patente", DbType.String, combustibleD.Patente);
                    DataBase.AddInParameter(cmd, "@Sucursal", DbType.Int16, combustibleD.Sucursal);
                    DataBase.AddInParameter(cmd, "@FechaD", DbType.DateTime, combustibleD.FechaCarga);
                    DataBase.AddInParameter(cmd, "@FechaH", DbType.DateTime, combustibleH.FechaCarga);
                    IDataReader Odbreader = DataBase.ExecuteReader(cmd);
                    while (Odbreader.Read())
                    {
                        Combustible combustible = new Combustible();
                        combustible.Patente = (string)Odbreader["Patente"];
                        combustible.Sucursal = (int)Odbreader["Sucursal"];
                        combustible.Item = (int)Odbreader["Item"];
                        combustible.FechaCarga = (DateTime)Odbreader["FechaCarga"];
                        combustible.Litros = (Decimal)Odbreader["Litros"];
                        combustible.Id_Usuario = (int)Odbreader["Id_Usuario"];

                        listCombustible.Add(combustible);
                    }

                    return listCombustible;
                }
            }
            catch (SqlException sql_ex)
            {
                ExceptionSQL exNew = new ExceptionSQL();
                throw new System.Exception(exNew.exSQL(sql_ex.ToString()).ToString());
            }
            catch (Exception ex)
            {
                ExceptionDAL exNew = new ExceptionDAL();
                throw new System.Exception(exNew.exDAL(ex.ToString()).ToString());
            }
        }
        #endregion
    }
}

